#!/usr/local/bin/python
#-*- encoding:utf-8 -*-
''' 
This script is designed to extract labeled results from all candidate results.
Input: All the results of queries
Output: The results of bm25 of title. The resuls for rmls to score.
'''
import sys

M = int(sys.argv[1]);

labeled_dict = {};
predictDict = {};
for i in range(M):
    queryID = str(i+1);
    labeled_dict[queryID] = {};
    predictDict[queryID] = [];

labeled = open('/home/wangshuxin/app_search/label_data/label_from_log.txt', 'r');

rmls_results = open('rmls_results', 'w');
c = 0;
for line in labeled:
    li = line.strip().split('|');
    c += 1;
    queryID = str(c);
    l = len(li);
    queryString = li[0];
    for i in range(1,l):
        cli = li[i].split(':');
        labeled_dict[queryID][cli[1]] = int(cli[2]);
        rmls_results.write('%s\t%s\t%s\t%s\n' % (queryID, queryString, cli[1], int(cli[2])));
labeled.close();

bm25_results = open('bm25_results', 'w');



predict = open('results', 'r');
for line in predict:
    li = line.strip().split('\t');
    queryID = li[0];
    appID = li[2]
    if appID in labeled_dict[queryID]:
        queryString = li[1];
        res = float(li[3]);
        if res > 0:
            bm25_results.write('%s\t%s\t%s\t%s\n' % (queryID, res, labeled_dict[queryID][appID], appID));
        else:
            pass;
predict.close();

bm25_results.close();
rmls_results.close();
